package com.heer.gymnasium.module.main.views;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.GridView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.main.adapters.HomeAdapter;
import com.heer.gymnasium.module.main.beans.CategoryBean;
import com.heer.imagepick.DensityUtils;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by zy on 2018/1/24.
 */

public class CategoryActivity extends BaseActivity {

    private final static int RES_ID_GRID_ITEM = R.layout.item_category;

    private final static int DRAWABLE_HOME = R.drawable.ic_main_home_normal;
    GridView gridView;

    private ArrayList<CategoryBean> categoryList;
    private QuickTypeAdapter<CategoryBean> categoryAdapter;
    private CategoryActivity $this;

    @Override
    protected void init() {
        super.init();
        gridView = new GridView(this);
        setContentView(gridView);
        gridView.setNumColumns(5);
        gridView.setPadding(0, DensityUtils.dip2px(this, 15), 0, 0);

        $this = this;
        categoryList = new ArrayList<>();
        categoryAdapter = new QuickTypeAdapter<CategoryBean>($this, categoryList, RES_ID_GRID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, CategoryBean data, int type, int pos) {
                HomeAdapter.bindCategoryItem(context, holder, data, pos, getCount());
            }
        };
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("分类");
        titleUtils.startMainActivity(this);
        gridView.setAdapter(categoryAdapter);
    }

    @Override
    protected void loadData() {
        super.loadData();
        getCategoryList();
    }

    //获取区域列表
    private void getCategoryList() {
        mAppHelper.apiService().getCategoryList("").enqueue(new BaseCallback<ApiResponse<List<CategoryBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<CategoryBean>>> call, Response<ApiResponse<List<CategoryBean>>> response) {
                categoryList.clear();
                categoryList.addAll(response.body().getData());
                categoryAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<ApiResponse<List<CategoryBean>>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, CategoryActivity.class));
    }
}
